{% extends "mp_base.html"%}
{% load commonFilters %}
{% load i18n %}

{% comment %}
copyright (c) 2009, paketka@gmail.com et. al
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

	* Redistributions of source code must retain the above copyright notice,
	  this list of conditions and the following disclaimer.

	* Redistributions in binary form must reproduce the above copyright notice,
	  this list of conditions and the following disclaimer in the documentation
	  and/or other materials provided with the distribution.

	* Neither the name of the <ORGANIZATION> nor the names of its contributors
	  may be used to endorse or promote products derived from this software
 	  without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
{% endcomment %}

{% comment %}
template, which renders html page with day/week/month overview for given MP
{% endcomment %}

{% block keywords %}
poslanec, {{ mp.name }} {{ mp.surname }}, poslankyně, zvolené období, detail, {{ mp.term.getStart }} - {{ mp.term.getEnd }},
funkční období, jak jsem hlasoval
{% endblock %}
{% block css-links %}
	<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}/css/ui.jqgrid.css" />
{% endblock %}

{% block js-links %}
	{{ block.super }}
	{% ifequal tabIndex 0 %}
		<script type="text/javascript" src="{{ MEDIA_URL }}/js/js/jqModal.js"></script>
		<script type="text/javascript" src="{{ MEDIA_URL }}/js/js/jqDnR.js"></script>
		<script type="text/javascript" src="{{ MEDIA_URL }}/js/js/jquery.jqGrid.js"></script>
	{% endifequal %}
{% endblock %}

{% block javascript-head %}

{% comment %}
	accordCharts is a javascript array of records.
	each record looks as follows:
		{	url: 'an ajax url where from chart should be fetched',
			optName: 'Option name for selection list'
			imgCache: 'store img element here once it's been fetched by ajax
			groupId: group.id
		}
{% endcomment %}
var accordCharts = [
			{
				url: '',
				optName: '{% trans "Vyber skupinu" %}',
				imgCache: '',
				groupId: '',
				groupSlug: ''
			}
	{% for i in mships %}
		{% if forloop.first %}
			,
		{% endif %}
		{% if forloop.last %}
			{
			{% if dateEnd %}
				url: '{% url get_mp_accordance mp.id, i.group.id, dateStart.year, dateStart.month, dateStart.day, noActive, dateEnd.year, dateEnd.month, dateEnd.day %}',
			{% else %}
				url: '{% url get_mp_accordance_day mp.id, i.group.id, dateStart.year, dateStart.month, dateStart.day, noActive %}',
			{% endif %}
			{% ifequal i.group.type "KLUB" %}
				optName: '{{ i.group.getPartyName }}',
			{% else %}
				optName: '{{ i.group.name|truncatewords:3 }}',
			{% endifequal %}
				imgCache: '',
				groupId: {{ i.group.id }},
				groupSlug: '{{ i.group.slug }}'
			}
		{% else %}
			{
			{% if dateEnd %}
				url: '{% url get_mp_accordance mp.id, i.group.id, dateStart.year, dateStart.month, dateStart.day, noActive, dateEnd.year, dateEnd.month, dateEnd.day %}',
			{% else %}
				url: '{% url get_mp_accordance_day mp.id, i.group.id, dateStart.year, dateStart.month, dateStart.day, noActive %}',
			{% endif %}
			{% ifequal i.group.type "KLUB" %}
				optName: '{{ i.group.getPartyName }}',
			{% else %}
				optName: '{{ i.group.name|truncatewords:3 }}',
			{% endifequal %}
				imgCache: '',
				groupId: {{ i.group.id }},
				groupSlug: '{{ i.group.slug }}'
			},
		{% endif %}
	{% endfor %}
	{% for group in parties %}
		{% comment %}
			we need to to add a ',' since list continues with parties now
		{% endcomment %}
		{% if forloop.first %}
			,
		{% endif %}
		{% if forloop.last %}
			{
			{% if dateEnd %}
				url: '{% url get_mp_accordance mp.id, group.id, dateStart.year, dateStart.month, dateStart.day, noActive, dateEnd.year, dateEnd.month, dateEnd.day %}',
			{% else %}
				url: '{% url get_mp_accordance_day mp.id, group.id, dateStart.year, dateStart.month, dateStart.day, noActive %}',
			{% endif %}
				optName: '{{ group.getPartyName }}',
				imgCache: '',
				groupId: {{ group.id }},
				groupSlug: '{{ group.slug }}'
			}
		{% else %}
			{
			{% if dateEnd %}
				url: '{% url get_mp_accordance mp.id, group.id, dateStart.year, dateStart.month, dateStart.day, noActive, dateEnd.year, dateEnd.month, dateEnd.day %}',
			{% else %}
				url: '{% url get_mp_accordance_day mp.id, group.id, dateStart.year, dateStart.month, dateStart.day, noActive %}',
			{% endif %}
				optName: '{{ group.getPartyName }}',
				imgCache: '',
				groupId: {{ group.id }},
				groupSlug: '{{ group.slug }}'
			},
		{% endif %}
	{% endfor %}
];

function groupChanged(elemId)
{
	var baseUrl = '';

	var divId = elemId.replace(/-.*$/g,'');

	newIndex = $('#' + elemId)[0].selectedIndex;

	if (newIndex == 0)
		return;

	if (accordCharts[newIndex].imgCache.charAt(0) == "<") {
		$('#' + divId).html(accordCharts[newIndex].imgCache);
		newUrl = '?accordGroup=' + accordCharts[newIndex].groupSlug;
		$('#link-me').attr('href', newUrl);
	}
	else {
		$('#' + divId).html('<img src="{{ MEDIA_URL }}/images/wait.gif"/>');
		$.ajax({
				url : accordCharts[newIndex].url,
				async : true,
				dataType : 'json',
				success : function(data) {
						newUrl = '?accordGroup=' + accordCharts[newIndex].groupSlug;
						$('#link-me').attr('href', newUrl);
						$('#' + divId).html(data);
						accordCharts[newIndex].imgCache = data;
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
						$('#' + divId).html('<img src="{{ MEDIA_URL }}/images/error.gif"/>');
				}
		});
	}
}

{% comment %}
Following block should be added into day view only
{% endcomment %}
{% ifequal tabIndex 0 %}
function createGrid(tableId, pagerId, url, caption)
{
	$(tableId).jqGrid({
				url: url,
				datatype: 'json',
				colNames: [	"{% trans "Číslo hlasování" %}",
							"{% trans "Název hlasování" %}",
							"{% trans "Výsledek" %}",
							"{% trans "Loajální" %}"
					],
				colModel :[
					{ name: "pollNo", index: "pollNo", width: 80, jsonmap: "pollNo", sortable: false },
					{ name: "title", index: "title", width: 200, jsonmap: "title", sortable: false },
					{ name: "result", index: "result", width: 120, jsonmap:"result", sortable: false },
					{ name: "loyal", index: "loyal", width: 80, jsonmap: "loyal", sortable: false}
				], 
				pager: $(pagerId),
				jsonReader: {
					root: "rows",
					id: "pk",
					page: "page",
					total: "total",
					records: "records",
					repeatitems: false
				},
				sortname:"start",
				sortorder:"desc",
				rowNum:10,
				rowList:[10,15,20],
				viewrecords:true,
				caption: caption,
				width:640,
				toolbar: [true,'top']
	});
	$(tableId).navGrid(pagerId,{edit:false,add:false,del:false});
}

var filterEnabled = false;

function filterProcedural(id)
{% comment %}
This is callback, that handles checkbox change events
{% endcomment %}
{
	if (filterEnabled) {
		filterEnabled = false;
	}
	else {
		filterEnabled = true;
	}

	grids = [];
	grids[0] = '#aye';
	grids[1] = '#nay';
	grids[2] = '#refrain';
	grids[3] = '#absences';

	{% comment %}
	We are going to retrieve Data URL for each particular grid
	{% endcomment %}
	for (i in grids) {
		$(grids[i]).setGridParam({
			url : getUrl(	$(grids[i]).getGridParam('url'))
		}).trigger("reloadGrid");
	}
}

function getUrl(urlStr)
{% comment %}
it checks if 'Proceduralni hlasovani' polls should be removed from grids,
if so, it then appends an option ?filterProcedural=1 to url.
{% endcomment %}
{
	var retVal = "";

	if (filterEnabled)
		retVal = urlStr + '?filterProcedural=1';
	else
		retVal = urlStr.replace(/\?filterProcedural=1/g,'');

	return retVal
}

{% endifequal %}

{% endblock javascript-head %}

{% block document-ready %}
	
	{{ block.super }}

	for (i in accordCharts) {
		$('#accordanceCharts-control').append(
			'<option value="' + accordCharts[i].groupId + '">' + accordCharts[i].optName + '</option>'
		);
	}

{% ifequal tabIndex 0 %}
	createGrid(
		"#aye",
		"#aye-pager",
		getUrl("{% url json.views.get_aye mp.slug date.year date.month date.day %}"),
		"{% trans "Návrhy poslancem podpořené" %}"
	);
	
	createGrid(
		"#nay",
		"#nay-pager",
		getUrl("{% url json.views.get_nay mp.slug date.year date.month date.day %}"),
		"{% trans "Návrhy poslancem zamítnuté" %}"
	);

	createGrid(
		"#refrain",
		"#refrain-pager",
		getUrl("{% url json.views.get_refrains mp.slug date.year date.month date.day %}"),
		"{% trans "Poslanec se zdržel hlasování" %}"
	);

	createGrid(
		"#absences",
		"#absences-pager",
		getUrl("{% url json.views.get_absences mp.slug date.year date.month date.day %}"),
		"{% trans "Hlasování, kdy poslanec nebyl přítomen" %}"
	);
{% endifequal %}

{% endblock document-ready %}

{% block left-sidebar %}
	{% comment %}
	We want to get rid off poll calendar for MP' complete term overivew.
	Instead of calendar we want to offer visitor the choice of other MP's terms
	{% endcomment %}
	<div class="ui-widget">
		<div class="ui-widget-header">
			<h1>{% trans "Kalendář hlasování" %}</h1>
		</div>
		<div class="ui-widget-content">
			<div class="container">
				{% url json.views.get_meetings_in_month date.year, date.month as events %}
				{% url psp.views.show_mp_days mp.slug, rangeType, date.year, date.month, date.day as eventDetails %}
				{% datepicker "meetingPicker" events eventDetails date %}
			</div>
		</div>
	</div>
{% endblock %}

{% block main-tab %}

<div class="ui-widget">
	<div class="ui-widget-header">
		<h2>{% trans "Členství v parlamentních organizacích" %}</h2>
	</div>
	<div class="ui-widget-content">
	<p style="font-size:0.9em">
		{% trans "V období" %} {{ mp.term.getStart }} - {{ mp.term.getEnd }} 
		{% trans "zasedám mimojiné i v těchto parlamentních skupinách a organizacích:" %}
	</p>
	{% for i in mships %}
		<div class="left" style="font-size:0.9em">
			<a href="{% url psp.views.show_group i.group.slug %}"> {{i.group.name}}</a>
		</div>
		<div class="right" style="font-size:0.9em">
			 [ {{ i.post }} {{ i.start|date:"Y m d" }} - {% if i.end %} {{ i.end|date:"Y m d" }} {% else %} {% now "Y m d" %} {% endif %}]
		</div>
		<div class="clear"></div>
		<p></p>
	{% endfor %}
	</ul>
	</div>
</div>

{% with mp.name|mf:"name" as mpName %}
{% with mp.surname|mf:"surname" as mpSurname %}
<div class="ui-widget">
	<div class="ui-widget-header">
		<h2>{% trans "Absence při hlasování" %}</h2>
	</div>
	<div class="ui-widget-content">
		<div class="left">
			{{ presenceChart.img|safe|mf:"presence-chart" }}
		</div>
		<div class="right">
			<p style="font-size:0.9em">
				{% ifequal mp.sex "F" %}
				{% blocktrans %}
				Koláčový graf vpravo upekla poslankyně {{ mpName }} {{ mpSurname }}.
				Koláč se skládá ze tří přísad:
				{% endblocktrans %}
				{% else %}
				{% blocktrans %}
				Koláčový graf vpravo upekl poslanec {{ mpName }} {{ mpSurname }}.
				Koláč se skládá ze tří přísad:
				{% endblocktrans %}
				{% endifequal %}
			</p>
				<ul style="font-size:0.9em">
					<li>{% trans "Účast při hlasování:" %} {{ mp.votes.presencePerc|floatformat:2|mf:"presence" }}%</li>
					<li>{% trans "Omluvená neúčast:" %} {{ mp.votes.excusedPerc|floatformat:2|mf:"excused" }}%</li>
					<li>{% trans "Neomluvená neúčast" %} {{ mp.votes.absencesPerc|floatformat:2|mf:"absence" }}%</li>
				</ul>
			<p style="font-size:0.9em">
				{% trans "Celková neúčast (omluvená + neomluvená)" %} {{ mp.votes.totalAbsencesPerc|floatformat:2|mf:"total-absence" }}%
			</p>
		</div>
		<div class="clear"></div>
	</div>
</div>

<div class="ui-widget">
	<div class="ui-widget-header">
		<h2>{% trans "Shoda hlasování" %}</h2>
	</div>
	<div class="ui-widget-content">
		<div class="left">
			<div class="center" id="accordanceCharts">
				{{ accordanceChart.img|safe|mf:"accordance-chart" }}
				{{ activeAccordance.img|safe|mf:"active-accordnace-chart" }}
			{% if partyPie %}
				{{ partyPie.img|safe|mf:"party-pie" }}
			{% endif %}
			</div>
			<div class="center groupSelect">
				<select name="accordanceCharts" id="accordanceCharts-control" onChange="groupChanged(this.id)">
				</select>
			</div>
		</div>
		<div class="right">
			<p style="font-size:0.9em"> 
				{% blocktrans %}
				Koláčové grafy vlevo znázorňují shodu hlasování s vybranou stranou,
				či parlamentní organizací jejímž je {{ mpName }} {{ mpSurname }}
				{% endblocktrans %}
				{% ifequal mp.sex "F" %}
				{% trans "členkou" %}.
				{% else %}
				{% trans "členem" %}.
				{% endifequal %}
				{% blocktrans %}
				Výpočet pro horní graf nezohledňuje přítomnost/nepřítomnost
				{% endblocktrans %}
				{% ifequal mp.sex "F" %}
				{% trans "poslankyně" %}
				{% else %}
				{% trans "poslance" %}
				{% endifequal %}
				{% blocktrans %}
				během jednotlivých hlasování.
				{% endblocktrans %}
			</p>
			{% if activeAccordance %}
			<p style="font-size:0.9em"> {% blocktrans %}
				Druhý graf již s touto nepřítomností počítá. Všechna hlasování,
				kterých se
				{% endblocktrans %}
				{% ifequal mp.sex "F" %}
				{% trans "poslankyně nezúčastnila, " %}
				{% else %}
				{% trans "poslanec nezúčastnil, " %}
				{% endifequal %}
				{% trans "nejsou do výsledku započtena." %}
			</p>
			{% endif %}
			<p style="font-size:0.9em"> {% blocktrans %}
				Graf shody s jinou parlamentní skupinou vyberete z roletky pod grafy. V nabídce
				se objeví všechny stranické kluby a k tomu další parlementní organizace, kterými
				je
				{% endblocktrans %}
				{% ifequal mp.sex "F" %}
				{% trans "poslankyně členkou." %}
				{% else %}
				{% trans "poslanec členem." %}
				{% endifequal %}
				{% trans "Pro jiné než stranické kluby (např. " %} {{ mp.partyName }}
				{% blocktrans %}) bude ve spodním grafu znázorněno partajní složení vybrané skupiny.
				{% endblocktrans %}
			</p>
			<p style="font-size:0.9em">
			{% if accordGroup %}
				<a href="?accordGroup={{ accordGroupSlug }}" id="link-me">link</a>
			{% else %}
				<a href="" id="link-me">link</a>
			{% endif %}
			</p>
		</div>
		<div class="clear"></div>
	</div>
</div>

{% endwith %}
{% endwith %}

{% ifequal tabIndex 0 %}
<div class "ui-widget">
	<div class="ui-widget-header">
		{% ifequal mp.sex "F" %}
			<h2>{% trans "Jak jsem hlasovala" %}</h2>
		{% else %}
			<h2>{% trans "Jak jsem hlasoval" %}</h2>
		{% endifequal %}
	</div>
	<div class="ui-widget-content">
		<p>
		<input type="checkbox" id="filterProcedural" onclick="filterProcedural(this.checked)">
			{% trans "Skryj procedurální hlasování" %}  <br/> 
		</p>

		<table id="aye" class="scroll" cellpadding="0" cellspacing="0">
		</table>
		<div id="aye-pager" class="scroll" style="text-align:center;">
		</div>

		<table id="nay" class="scroll" cellpadding="0" cellspacing="0"> 
		</table>
		<div id="nay-pager" class="scroll" style="text-align:center;">
		</div>

		<table id="refrain" class="scroll" cellpadding="0" cellspacing="0"> 
		</table>
		<div id="refrain-pager" class="scroll" style="text-align:center;">
		</div>

		<table id="absences" class="scroll" cellpadding="0" cellspacing="0"> 
		</table>
		<div id="absences-pager" class="scroll" style="text-align:center;">
		</div>
	</div>
</div>
{% endifequal %}
{% endblock main-tab %}

